package _01_event_bus._1_0_1_subchannel

import akka.actor.ActorRef
import akka.event.{EventBus, SubchannelClassification}
import akka.util.Subclassification

/**
  * @author galudisu
  */
final case class MsgEnvelope(topic: String, payload: Any)

class StartsWithSubclassification extends Subclassification[String] {
  override def isEqual(x: String, y: String): Boolean =
    x == y
  override def isSubclass(x: String, y: String): Boolean =
    x.startsWith(y)
}

class SubchannelBusImpl extends EventBus with SubchannelClassification {

  override type Event = MsgEnvelope
  override type Classifier = String
  override type Subscriber = ActorRef
  override protected implicit def subclassification: Subclassification[Classifier] =
    new StartsWithSubclassification
  override protected def classify(event: MsgEnvelope): String = event.topic
  override protected def publish(event: MsgEnvelope, subscriber: ActorRef): Unit = {
    subscriber ! event.payload
  }
}
